package com.fruit.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class DataUtil {

	public static BigDecimal getBigDecimal(Object o) {
		try{
		if (o == null)
			return new BigDecimal(0);
		if (o instanceof BigDecimal) {
			return (BigDecimal) o;
		}else if(o instanceof Integer){ 
			return new BigDecimal((Integer)o);
		}else if(o instanceof Double){ 
			return new BigDecimal((Double)o);
		}else if(o instanceof Long){ 
			return new BigDecimal((Long)o);
		}else if(o instanceof String){ 
			return new BigDecimal((String)o);
		}else {
			return new BigDecimal(0);
		}
		}catch(Exception e){
			return new BigDecimal(0);
		}
	}
	
	public static BigDecimal getBigDecimal(Object o, int scale) {
		try{
		if (o == null)
			return new BigDecimal(0).setScale(scale,BigDecimal.ROUND_DOWN);
		if (o instanceof BigDecimal) {
			return ((BigDecimal) o).setScale(scale,BigDecimal.ROUND_DOWN);
		}else if(o instanceof Integer){ 
			return new BigDecimal((Integer)o).setScale(scale,BigDecimal.ROUND_DOWN);
		}else if(o instanceof Double){ 
			return new BigDecimal((Double)o).setScale(scale,BigDecimal.ROUND_DOWN);
		}else if(o instanceof Float){ 
			return new BigDecimal((Float)o).setScale(scale,BigDecimal.ROUND_DOWN);
		}else if(o instanceof Long){ 
			return new BigDecimal((Long)o).setScale(scale,BigDecimal.ROUND_DOWN);
		}else if(o instanceof String){ 
			return new BigDecimal((String)o).setScale(scale,BigDecimal.ROUND_DOWN);
		}else {
			return new BigDecimal(0).setScale(scale,BigDecimal.ROUND_DOWN);
		}
		}catch(Exception e){
			return new BigDecimal(0).setScale(scale,BigDecimal.ROUND_DOWN);
		}
	}
	
	public static BigDecimal mul(int scale,Object ... obj){
		BigDecimal result = BigDecimal.ONE;
		for(Object o:obj){
			result = result.multiply(getBigDecimal(o));
		}
		return result.setScale(scale, BigDecimal.ROUND_HALF_UP);
	}
	
	public static BigDecimal add(int scale,Object ... obj){
		BigDecimal result = BigDecimal.ZERO;
		for(Object o:obj){
			result = result.add(getBigDecimal(o));
		}
		return result.setScale(scale, BigDecimal.ROUND_HALF_UP);
	}
	
	
	public static void main(String args[]){
		List<Long> listL = new ArrayList<Long>();
		System.out.println("bbbb:"+System.currentTimeMillis());
		for(long i=1000000000000L ; i<1000000000100L; i++){
			
			listL.add(i);
		}
		System.out.println("aaaa:"+System.currentTimeMillis());
		System.out.println(StringUtils.join(listL, ","));
	}
}
